<?php

class CommodityCategory_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return CommodityCategory
     */
    public static function getCommodityCategoryByRow($row)
    {
        $thatCommodityCategory = new CommodityCategory;
        $thatCommodityCategory->setId($row->id);
        $thatCommodityCategory->setFatherId($row->father_id);
        $thatCommodityCategory->setName($row->name);
        $thatCommodityCategory->setIconUrl($row->icon_url);
        return $thatCommodityCategory;
    }

    /**
     * @param CommodityCategory $thatCommodityCategory
     * @return array
     */
    public static function getModelByCommodityCategory($thatCommodityCategory)
    {
        return array(
            'father_id' => $thatCommodityCategory->getFatherId(),
            'name'      => $thatCommodityCategory->getName(),
            'icon_url'  => $thatCommodityCategory->getIconUrl(),
        );
    }

    /**
     * @param int $id
     * @return CommodityCategory
     */
    public static function getCommodityCategoryById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_cat.*');
        $CI->db->from('commodity_cat');
        $CI->db->where('commodity_cat.id', $id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getCommodityCategoryByRow($row);
        else
            return null;
    }

    /**
     * @param string $name
     * @return CommodityCategory
     */
    public static function getCommodityCategoryByName($name)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_cat.*');
        $CI->db->from('commodity_cat');
        $CI->db->where('commodity_cat.name', $name);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getCommodityCategoryByRow($row);
        else
            return null;
    }

    /**
     * @return array
     */
    public static function getCommodityCategoryArray()
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_cat.*');
        $CI->db->from('commodity_cat');
        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityCategoryByRow($row);
        }
        return $array;
    }

    /**
     * @return array
     */
    public static function getMasterCommodityCategoryArray()
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_cat.*');
        $CI->db->from('commodity_cat');
        $CI->db->where('commodity_cat.father_id', 0);
        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityCategoryByRow($row);
        }
        return $array;
    }

    /**
     * @return array
     */
    public static function getNotMasterCommodityCategoryArray()
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_cat.*');
        $CI->db->from('commodity_cat');
        $CI->db->where('commodity_cat.father_id !=', 0);
        $CI->db->order_by('id', 'desc');
        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityCategoryByRow($row);
        }
        return $array;
    }

    /**
     * @param int $id
     * @return array
     */
    public static function getChildrenCommodityCategoryArrayById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_cat.*');
        $CI->db->from('commodity_cat');
        $CI->db->where('commodity_cat.father_id', $id);
        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityCategoryByRow($row);
        }
        return $array;
    }


    /**
     * @param int $id
     * @return array
     */
    public static function getDescendantCommodityCategoryIdArrayById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_cat.*');
        $CI->db->from('commodity_cat');
        $CI->db->where('commodity_cat.father_id', $id);
        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $thisCatId = $row->id;
            $array     = array_merge($array, self::getDescendantCommodityCategoryIdArrayById($thisCatId));
            $array[]   = $thisCatId;
        }
        return $array;
    }

    /**
     * @param CommodityCategory $thatCommodityCategory
     * @throws DatabaseException
     * @return CommodityCategory
     */
    public static function add($thatCommodityCategory)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('commodity_cat', self::getModelByCommodityCategory($thatCommodityCategory));
        if ($result)
            return self::getCommodityCategoryById($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param CommodityCategory $thatCommodityCategory
     * @throws DatabaseException
     * @return CommodityCategory
     */
    public static function update($thatCommodityCategory)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatCommodityCategory->getID());
        $result = $CI->db->update('commodity_cat', self::getModelByCommodityCategory($thatCommodityCategory));
        if ($result)
            return self::getCommodityCategoryById($thatCommodityCategory->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param CommodityCategory $thatCommodityCategory
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatCommodityCategory)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatCommodityCategory->getID());
        $result = $CI->db->delete('commodity_cat');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

}
